package com.hryt.firstandroid;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "====Main====";

    private Button mBtnStartStandard;
    private Button mBtnStartSingleTop;
    private Button mBtnStartSingleTask;
    private Button mBtnStartSingleInstance;
    private Button mBtnStartService;
    private Button mBtnStopService;
    private Button mBtnBindService;
    private Button mBtnUnbindService;
    private Button mBtnSendBroadcast;

    private MyService service;
    private boolean isConnected = false;
    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            isConnected = true;
            MyService.MyBinder binder = (MyService.MyBinder) iBinder;
            service = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            isConnected = false;
            service = null;
        }
    };

    private TestBroadcastReceiver mReceiver = new TestBroadcastReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        mBtnStartStandard = findViewById(R.id.btn_standard);
        mBtnStartStandard.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, StandardActivity.class);
            startActivity(intent);
        });

        mBtnStartSingleTop = findViewById(R.id.btn_single_top);
        mBtnStartSingleTop.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, SingleTopActivity.class);
            startActivity(intent);
        });

        mBtnStartSingleTask = findViewById(R.id.btn_single_task);
        mBtnStartSingleTask.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, SingleTaskActivity.class);
            startActivity(intent);
        });

        mBtnStartSingleInstance = findViewById(R.id.btn_single_instance);
        mBtnStartSingleInstance.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, SingleInstanceActivity.class);
            startActivity(intent);
        });

        mBtnStartService = findViewById(R.id.btn_start_service);
        mBtnStartService.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, MyService.class);
            startService(intent);
        });

        mBtnStopService = findViewById(R.id.btn_stop_service);
        mBtnStopService.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, MyService.class);
            stopService(intent);
        });

        mBtnBindService = findViewById(R.id.btn_bind_service);
        mBtnBindService.setOnClickListener(view -> {
            if (!isConnected) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                bindService(intent, sc, Context.BIND_AUTO_CREATE);
            }
        });

        mBtnUnbindService = findViewById(R.id.btn_unbind_service);
        mBtnUnbindService.setOnClickListener(view -> {
            if (isConnected) {
                unbindService(sc);
                isConnected = false;
            }
        });

        mBtnSendBroadcast = findViewById(R.id.btn_send_broad);
        mBtnSendBroadcast.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setAction(TestBroadcastReceiver.ACTION_TEST);
//            intent.setComponent(new ComponentName("com.hryt.firstandroid",
//                    "com.hryt.firstandroid.TestBroadcastReceiver"));
            sendBroadcast(intent);
        });

        IntentFilter filter = new IntentFilter();
        filter.addAction(TestBroadcastReceiver.ACTION_TEST);
        registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        unregisterReceiver(mReceiver);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}